﻿/*******************************************************
Copyright © 2007-2009 Active Technologies Group, Inc. (ATGi)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*******************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommunityServer.Components;
using Telligent.DynamicConfiguration.Components;
using CommunityServer.Controls;
using System.Web.UI;

namespace ATGi.CS.BrightCove.Controls
{
    public class BrightCoveVideoContentFragment : ConfigurableContentFragmentBase
    {
        public override void AddContentControls(System.Web.UI.Control control)
        {
            BCSortOrder order = (BCSortOrder)Enum.Parse(typeof(BCSortOrder),GetStringValue("sortOrder","ASC"));
            BCSortByType type = (BCSortByType)Enum.Parse(typeof(BCSortByType), GetStringValue("sortBy", "PUBLISH_DATE"));
            BrightCoveVideoList list = new BrightCoveVideoList();
            BrightCoveVideoQuery query = new BrightCoveVideoQuery();
            query.SortBy = type;
            query.SortOrder = order;
            query.Fields = new List<string>() { "id","name","shortDescription","thumbnailURL","FLVURL" };
            query.PageSize = GetIntValue("itemsInList", 5);
            list.QueryOverrides = query;
            list.ItemTemplate = new ItemTemplate(new ItemTemplate.DataBinder(BindDefaultContent), new ItemTemplate.ChildControlCreator(CreateDefaultContentControls));
            list.Tag = (GetBoolValue("showThumbnail",false)) ? WrappedControlTag.Div : WrappedControlTag.Ul;
            list.CssClass = GetStringValue("cssClass", "CommonSidebarList");
            control.Controls.Add(list);
        }

        protected void BindDefaultContent(Control control, IDataItemContainer dataItemContainer)
        {
            BrightCoveVideoLinkTo linkTarget = (BrightCoveVideoLinkTo)Enum.Parse(typeof(BrightCoveVideoLinkTo), GetStringValue("linkTarget", BrightCoveVideoLinkTo.Video.ToString()));
            string displayProperty = GetStringValue("displayProperty", "Name");
            if (GetBoolValue("showThumbnail", false))
            {
                double scaleFactor = (displayProperty.ToLower().Equals("none")) ? 1.25 : 1.75;
                control.Controls.Add(new LiteralControl("<div style=\"margin:8px 8px 0 0;padding:0;width:"));
                control.Controls.Add(new LiteralControl(GetIntValue("thumbnailWidth", 120).ToString()));
                control.Controls.Add(new LiteralControl("px;height:"));
                control.Controls.Add(new LiteralControl(((Math.Round(GetIntValue("thumbnailHeight", 90) * scaleFactor,0,MidpointRounding.ToEven))).ToString()));
                control.Controls.Add(new LiteralControl("px;text-align:center;overflow:hidden;float:left;font-size:80%;font-weight:bold;word-wrap:break-word;\">"));

                BrightCoveVideoData linkData = new BrightCoveVideoData();
                linkData.LinkTo = linkTarget;
                linkData.Tag = WrappedControlTag.Div;
                linkData.Attributes["style"] = string.Format("width:{0}px;height:{1}px;margin-bottom:4px;overflow:hidden;", GetIntValue("thumbnailWidth", 120).ToString(), GetIntValue("thumbnailHeight", 90).ToString());
                linkData.ContentTemplate = new ItemTemplate(new ItemTemplate.DataBinder(BindThumbnailContent), new ItemTemplate.ChildControlCreator(CreateDefaultContentControls));
                control.Controls.Add(linkData);
                AddVideoData(control, displayProperty, linkTarget);
                control.Controls.Add(new LiteralControl("</div>"));
            }
            else
            {
                control.Controls.Add(new LiteralControl("<li>"));
                AddVideoData(control, displayProperty, linkTarget);
                control.Controls.Add(new LiteralControl("</li>"));
            }
        }

        private void AddVideoData(Control control, string property, BrightCoveVideoLinkTo linkTarget)
        {
            if (property.ToLower().Equals("none"))
                return;
            BrightCoveVideoData videoData = new BrightCoveVideoData();
            videoData.Property = property;
            videoData.LinkTo = linkTarget;
            control.Controls.Add(videoData);
        }

        protected void CreateDefaultContentControls(Control control)
        {
            // do nothing
        }
        
        protected void BindThumbnailContent(Control control, IDataItemContainer dataItemContainer)
        {
            BrightCoveVideoImage thumbnail = new BrightCoveVideoImage();
            thumbnail.Width = GetIntValue("thumbnailWidth", 120);
            thumbnail.Height = GetIntValue("thumbnailHeight", 90);
            thumbnail.BorderWidth = 0;

            control.Controls.Add(thumbnail);
        }

        public override string FragmentDescription
        {
            get { return BrightCoveConfiguration.Instance.FragmentDefaults.Description; }
        }

        public override string FragmentName
        {
            get { return GetStringValue("fragmentTitle",BrightCoveConfiguration.Instance.FragmentDefaults.Name); }
        }

        public override Telligent.DynamicConfiguration.Components.PropertyGroup[] GetPropertyGroups()
        {
            PropertyGroup[] propertyGroups = new PropertyGroup[] { 
                new PropertyGroup("options", ResourceManager.GetString("Options"), 1),
                new PropertyGroup("style",ResourceManager.GetString("Style"),2)
            };
            propertyGroups[0].Properties.Add(new Property("fragmentTitle","List Title", PropertyType.String, 0, BrightCoveConfiguration.Instance.FragmentDefaults.Name));
            propertyGroups[0].Properties.Add(new Property("itemsInList", "Number of Items to Display", PropertyType.Int, 1, BrightCoveConfiguration.Instance.FragmentDefaults.ListSize.ToString()));
            propertyGroups[0].Properties.Add(new Property("showThumbnail", "Display with thumbnail?", PropertyType.Bool, 2, BrightCoveConfiguration.Instance.FragmentDefaults.ShowThumbnail.ToString()));
            Property displayProp = new Property("displayProperty", "Display Property", PropertyType.String, 3, "Name");
            displayProp.SelectableValues.Add(new PropertyValue("Name", "Video Name", 1));
            displayProp.SelectableValues.Add(new PropertyValue("ShortDescription","Short Description",2));
            displayProp.SelectableValues.Add(new PropertyValue("SourceURL", "Video Url", 3));
            displayProp.SelectableValues.Add(new PropertyValue("None", "No Text", 4));
            propertyGroups[0].Properties.Add(displayProp);
            Property linkProp = new Property("linkTarget", "Display Item Link", PropertyType.String, 4, BrightCoveVideoLinkTo.Video.ToString());
            linkProp.SelectableValues.Add(new PropertyValue(BrightCoveVideoLinkTo.Video.ToString(), "Video", 1));
            linkProp.SelectableValues.Add(new PropertyValue(BrightCoveVideoLinkTo.FlashDownload.ToString(), "Flash Download", 2));
            linkProp.SelectableValues.Add(new PropertyValue(BrightCoveVideoLinkTo.Link.ToString(), "Link", 3));
            linkProp.SelectableValues.Add(new PropertyValue(BrightCoveVideoLinkTo.Nothing.ToString(), "Nothing", 4));
            propertyGroups[0].Properties.Add(linkProp);
            Property p = new Property("sortOrder", "Sort Order", PropertyType.String, 5, BrightCoveConfiguration.Instance.FragmentDefaults.SortOrder.ToString());
            p.SelectableValues.Add(new PropertyValue(BCSortOrder.ASC.ToString(),"Ascending", 1));
            p.SelectableValues.Add(new PropertyValue(BCSortOrder.DESC.ToString(),"Descending",2));
            propertyGroups[0].Properties.Add(p);
            Property p2 = new Property("sortBy","Sort By",PropertyType.String,6,BrightCoveConfiguration.Instance.FragmentDefaults.SortBy.ToString());
            p2.SelectableValues.Add(new PropertyValue(BCSortByType.CREATION_DATE.ToString(),"Date Created",1));
            p2.SelectableValues.Add(new PropertyValue(BCSortByType.MODIFIED_DATE.ToString(), "Date Modified", 2));
            p2.SelectableValues.Add(new PropertyValue(BCSortByType.PLAYS_TOTAL.ToString(), "Total Plays", 3));
            p2.SelectableValues.Add(new PropertyValue(BCSortByType.PLAYS_TRAILING_WEEK.ToString(), "Plays Last 7 Days", 4));
            p2.SelectableValues.Add(new PropertyValue(BCSortByType.PUBLISH_DATE.ToString(), "Date Published", 5));
            propertyGroups[0].Properties.Add(p2);

            propertyGroups[1].Properties.Add(new Property("listStyle", "List CSS Class", PropertyType.String, 1, "CommonSidebarList"));
            propertyGroups[1].Properties.Add(new Property("thumbnailWidth","Thumbnail Width",PropertyType.Int,2,"120"));
            propertyGroups[1].Properties.Add(new Property("thumbnailHeight","Thumbnail Height",PropertyType.Int,3,"90"));
            return propertyGroups;
        }
    }
}
